package com.slider.sun;

import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.Toast;

import com.daimajia.slider.library.SliderLayout;
import com.daimajia.slider.library.SliderTypes.DefaultSliderView;

import java.io.File;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity {
    private View mContentView;
    private SliderLayout sliderShow;
    private File[] files;
    private String number;
    private String[] images;
    Handler handler=new Handler();


    Runnable runnable=new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            //要做的事情
            initSlider();
            handler.postDelayed(this, 30000);
        }
    };
    private boolean flag;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        mContentView = findViewById(R.id.fullscreen_content);
        sliderShow = findViewById(R.id.slider);
        number = SharedPreferencesUtils.getString(this, "number", "");
        if (TextUtils.isEmpty(number)) {
            showInputDialog();
        } else {
            handler.postDelayed(runnable, 10000);//每两秒执行一次runnable.
            initSlider();
        }
//        mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE
//                | View.SYSTEM_UI_FLAG_FULLSCREEN
//                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
//                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
//                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
    }

    private void initSlider() {
        ApiUtils.getInstance().banner_select(number, new ApiListener<BannerRet.DataBean>() {
            @Override
            public void onSuccess(BannerRet.DataBean dataBean) {
                if(SharedPreferencesUtils.getInt(FullscreenActivity.this,"updatetime",0)!=dataBean.getUpdatetime()){
                    SharedPreferencesUtils.setInt(FullscreenActivity.this,"updatetime",dataBean.getUpdatetime());
                    sliderShow.removeAllSliders();
                    initdata(dataBean);
                }else {
                    if(!flag){
                        initdata(dataBean);
                    }
//                    Log.e("TAG","??"+sliderShow.getCurrentPosition());
                }
            }

            @Override
            public void onFailed(String errorMsg) {
                Toast.makeText(FullscreenActivity.this,
                        errorMsg,
                        Toast.LENGTH_SHORT).show();
            }
        });

    }

    private void initdata(BannerRet.DataBean dataBean) {
        flag=true;
        if(dataBean.getBanner_images().contains(",")){
            images=dataBean.getBanner_images().split(",");
        }else {
            if(TextUtils.isEmpty(dataBean.getBanner_images())){
                MyUtils.showToast(FullscreenActivity.this,"轮播图未上传");
                return;
            }
            images=new String[1];
            images[0]=dataBean.getBanner_images();
        }
        for (String url:images){
            DefaultSliderView defaultSliderView = new DefaultSliderView(FullscreenActivity.this);
            defaultSliderView.image(MyUtils.formatUrl(url));
//                    defaultSliderView.image("http://my-doorbell.oss-cn-beijing.aliyuncs.com/alioss/20181218/d3f112bf28b94bd5bea2a513bf37ee8c.jpg");
            Log.e("TAG",MyUtils.formatUrl(url));
            if(dataBean.getBrowserswitch()>0){
//                        defaultSliderView.setOnSliderClickListener(new BaseSliderView.OnSliderClickListener() {
//                            @Override
//                            public void onSliderClick(BaseSliderView slider) {
//                                startActivity(new Intent(FullscreenActivity.this,FullscreenBrowserActivity.class));
//                            }
//                        });
            }
            sliderShow.addSlider(defaultSliderView);
        }
        sliderShow.setPresetTransformer(SliderLayout.Transformer.Default);
        if(images.length>1){
            sliderShow.startAutoCycle();
            sliderShow.setDuration(4000);
        }else {
//            sliderShow.setDuration(4000*1000);
            sliderShow.stopAutoCycle();
        }

    }

    private void showInputDialog() {
        final EditText editText = new EditText(this);
        AlertDialog.Builder inputDialog =
                new AlertDialog.Builder(this);
        inputDialog.setTitle("请输入设备编号").setView(editText);
        inputDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(FullscreenActivity.this,
                                editText.getText().toString(),
                                Toast.LENGTH_SHORT).show();
                        number = editText.getText().toString();
                        ApiUtils.getInstance().banner_add(number, new ApiListener<String>() {
                            @Override
                            public void onSuccess(String s) {
                                SharedPreferencesUtils.setString(FullscreenActivity.this, "number", number);
                                initSlider();
                                handler.postDelayed(runnable, 10000);//每两秒执行一次runnable.
                            }

                            @Override
                            public void onFailed(String errorMsg) {
                                showInputDialog();
                                Toast.makeText(FullscreenActivity.this,
                                        errorMsg,
                                        Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                }).show();
    }
}